'''
Created on 2010-3-2

@author: Administrator
'''
from jolly.datastructures.Queue import Queue
from jolly.datastructures.Deque import Deque
from jolly.datastructures.QueueAsLinkedList import QueueAsLinkedList
from jolly.datastructures.Exception import *

class DequeAsLinkedList(QueueAsLinkedList, Deque):
    '''
    Deque implemented using a linked list.
    '''


    def __init__(self):
        '''
        (DequeAsLinkedList)->None
        Constructs a deque
        '''
        super(DequeAsLinkedList, self).__init__()
    
    def enqueueHead(self, obj):
        '''
        (DequeAsLinkedList, Object)->None
        Enqueues the given object at the head of this deque.
        '''
        self._list.prepend(obj)
        self._count+=1
        
    def getTail(self):
        '''
        (DequeAsLinkedList)->Object
        Returns the object at the tail of this deque.
        '''
        if self._count==0:
            raise ContainerEmpty
        return self._list.last.datum
    
    def dequeueTail(self):
        '''
        (DequeAsLinkedList)->Object
        Dequeues the object at the tail of this deque.
        '''
        if self._count == 0:
            raise ContainerEmpty
        result=self._list.last.datum
        self._list.extract(result)
        self._count-=1
        return result
    
    @staticmethod
    def main(*argv):
        "DequeAsLinkedList test program"
        print DequeAsLinkedList.main.__doc__
        deque2 = DequeAsLinkedList()
        Queue.test(deque2)
        Deque.test(deque2)
        return 0
    
if __name__ == "__main__":
    sys.exit(DequeAsLinkedList.main(sys.argv))